package com.ll.listdemo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘龙
 * @version 1.0
 */
public class ListExercise02 {
    public static void main(String[] args) {
        List<Book> list = new ArrayList<Book>();
        list.add(new Book("java", 999.0, "ll"));
        list.add(new Book("spring", 100.0, "ll2"));
        list.add(new Book("boot", 600.0, "ll3"));

        for (Book book : list) {
            System.out.println(book);
        }

        // 冒泡排序
        sort(list);
        System.out.println("============");

        for (Book book : list) {
            System.out.println(book);
        }

    }

    public static void sort(List<Book> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int i1 = 0; i1 < list.size() - 1 - i; i1++) {
                // 取出对象
                Book book = list.get(i1);
                Book book2 = list.get(i1 + 1);
                if (book.getPrice() > book2.getPrice()) {
                    list.set(i1, book2);
                    list.set(i1+1, book);
                }
            }
        }
    }
}

class Book {
    private String bookName;
    private Double price;
    private String author;

    public Book(String bookName, Double price, String author) {
        this.bookName = bookName;
        this.price = price;
        this.author = author;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "名称: " + bookName + "\t\t" + "价格: " + price +
                "\t\t" + "作者: " + author;
    }
}